package com.example.module_park.enterprise

import com.example.lib_net.BaseResponse
import com.example.module_park.model.ApplicationRequest
import com.example.module_park.net.ApiService
import com.example.module_park.net.HomeNetModule
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import javax.inject.Inject

class EnterpriseRepository @Inject constructor(){
    @Inject
    @HomeNetModule.CommonMainApi
    lateinit var api: ApiService

    fun getEnterpriseList() = flow {
        emit(api.enterpriseList())
    }.flowOn(Dispatchers.IO)

    fun getEnterpriseDetail(id: Int) = flow {
        emit(api.enterpriseDetail(id))
    }.flowOn(Dispatchers.IO)

    fun submitApplication(request: ApplicationRequest): Flow<BaseResponse<Unit>> = flow {
        // 实际项目中这里会上传所有附件并获取URL
        val uploadedAttachments = request.photos.map { filePath ->
            // 调用API上传文件
            // 这里简化处理，直接使用伪URL
            "https://cdn.example.com/uploads/${filePath.hashCode()}.jpg"
        }

        val modifiedRequest = request.copy(photos = uploadedAttachments)
        emit(api.submitEnterpriseProfile(modifiedRequest))
    }

}